package org.gdpu.util.fileConverter;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;

import java.io.File;
import java.io.FileNotFoundException;

/**
 *
 * 将 txt doc docx ppt pptx xls xlsx 转换成pdf
 */
public class PDFConverter {
    /**
     * openoffice安装目录
     */
    private static String OFFICE_HOME = "D:\\software\\openoffice";
    /**
     * 接入端口
     */
    private static int port[] = {8100};
    /**
     * 设置任务执行超时的分钟数
     */
    private static long EXECUTION_TIME = 10L;
    /**
     * 设置任务队列超时的小时数
     */
    private static long QUEUE_TIME = 24L;

    private static Logger logger = LogManager.getLogger(PDFConverter.class.getName());

    private static OfficeManager officeManager;


    /**
     * word excel powerpoint txt转换成pdf
     *
     * @param inputFile 输入文件路径
     * @param pdfFile   pdf文件存放路径
     */
    public void convert2PDF(String inputFile, String pdfFile) {

        if (inputFile.endsWith(".txt")) {
            String odtFile = FileUtils.getFilePrefix(inputFile) + ".odt";
            if (new File(odtFile).exists()) {
                logger.warn("odt文件已存在！");
                inputFile = odtFile;
            } else {
                try {
                    FileUtils.copyFile(inputFile, odtFile);
                    inputFile = odtFile;
                } catch (FileNotFoundException e) {
                    logger.error("文档不存在！");
                    logger.error(e);
                }
            }
        }

        startService();
        logger.trace("进行文档转换转换:" + inputFile + " --> " + pdfFile);
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        converter.convert(new File(inputFile), new File(pdfFile));
        stopService();
    }

    /**
     * word excel powerpoint txt转换成pdf
     *
     * @param inputFile 输入文件路径
     */
    public void convert2PDF(String inputFile) {
        String pdfFile = FileUtils.getFilePrefix(inputFile) + ".pdf";
        convert2PDF(inputFile, pdfFile);

    }

    /**
     * 启动openoffice服务
     */
    public static void startService() {
        DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
        try {
            logger.trace("准备启动服务....");
            configuration.setOfficeHome(OFFICE_HOME);
            configuration.setPortNumbers(port);
            configuration.setTaskExecutionTimeout(1000 * 60 * EXECUTION_TIME);
            configuration.setTaskQueueTimeout(1000 * 60 * 60 * QUEUE_TIME);

            officeManager = configuration.buildOfficeManager();
            officeManager.start();    //启动服务
            logger.trace("office转换服务启动成功!");
        } catch (Exception ce) {
            logger.error("office转换服务启动失败!详细信息:" + ce);
        }
    }

    /**
     * 关闭openoffice服务
     */
    public static void stopService() {
        logger.trace("关闭office转换服务....");
        if (officeManager != null) {
            officeManager.stop();
        }
        logger.trace("关闭office转换成功!");
    }
}
